<?php
/**
 * Karm Model
 * @author Sokha RUM
 */
class m_translate extends Zmodel {
	private $db2;
	/**
	 * Constructor of Karm Model
	 */
    function __construct() {
    	$this->db2 = $this->load->database("translate", true);
        parent::__construct("translate");
    }
    
    /**
     * getting list of language with the specific app_id
     * @param int $app_id
     */
    function getLanguages($app_id) {
        $sql = "SELECT lan_id, lan_code, lan_desc FROM languages WHERE app_id = ". $app_id;
        $query = $this->db2->query($sql);
        //return dbCiToArray($query, "lan_code");
        $result = array();
        foreach($query->result_array() as $row) {
            $result[$row["lan_code"]] = $row["lan_id"];
        }
        return $result;
    }
    
    /**
     * Getting default language with the specific application
     * @param unknown_type $app_id
     */
    function getDefaultLanuage($app_id) {
    	//
        $sql = "SELECT lan_id, lan_code FROM languages 
                                WHERE app_id = ".$app_id." AND  
                                      lan_default = 1";
        $query = $this->db2->query($sql);
    	$nb_rows = $query->num_rows();
        if ($nb_rows < 1) {
            return NULL;
        } else {
            $row = $query->row_array();
            return $row;
        }
    }
    
    /**
     * Getting application id
     * @param unknown_type $app_code
     */
    function getApplicationId($app_code) {
        $sql = "SELECT app_id FROM applications 
                              WHERE app_code = '".pg_escape_string($app_code)."'";
    	$query = $this->db2->query($sql);
    	$nb_rows = $query->num_rows();
        if ($nb_rows < 1) {
            return NULL;
        } else {
            $row = $query->row_array();
            return $row["app_id"];
        }
    }
    
    /**
     * 
     * Getting list of translation object
     * @param String $page_code
     * @param int $lan_id
     */
    function getObjects($page_code, $lan_id) {
    	$page_code_sql = "";
    	foreach ($page_code as $code) {
    	    $page_code_sql .= "'".pg_escape_string($code)."',";
    	}
    	$page_code_sql = trim($page_code_sql, ",");
        $sql = "SELECT o.obj_code,
                       ov.obv_value
                FROM pages p, objects o, object_value ov
                WHERE p.pag_code in (".$page_code_sql.") AND
                      p.pag_id = o.pag_id AND
                      o.obj_id = ov.obj_id AND
                      ov.lan_id = ".$lan_id;
         $query = $this->db2->query($sql);
         $result = array();
         foreach($query->result_array() as $row) {
         	 $result[$row["obj_code"]] = $row["obv_value"];
         }
         return $result; 
    }

}